<?php

/**
 * This is the model class for table "{{orders}}".
 *
 * The followings are the available columns in table '{{orders}}':
 * @property string $id
 * @property string $order_id
 * @property integer $city_id
 * @property integer $user_id
 * @property string $name
 * @property string $phone
 * @property string $title
 * @property string $url
 * @property string $thumb
 * @property string $order_type
 * @property string $description
 * @property integer $create_time
 * @property integer $quantity
 * @property string $price
 * @property string $amount
 * @property string $discount
 * @property integer $status
 * @property integer $payment_time
 */
class Orders extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{orders}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('order_id, city_id, user_id, name, phone, title, url, thumb, order_type, create_time, quantity, price, amount, discount, status, payment_time', 'required'),
			array('city_id, user_id, create_time, quantity, status, payment_time', 'numerical', 'integerOnly'=>true),
			array('order_id, name, phone', 'length', 'max'=>20),
			array('title', 'length', 'max'=>50),
			array('url, description', 'length', 'max'=>200),
			array('thumb', 'length', 'max'=>120),
			array('order_type', 'length', 'max'=>7),
			array('price, amount, discount', 'length', 'max'=>10),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, order_id, city_id, user_id, name, phone, title, url, thumb, order_type, description, create_time, quantity, price, amount, discount, status, payment_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'order_id' => 'Order',
			'city_id' => 'City',
			'user_id' => 'User',
			'name' => 'Name',
			'phone' => 'Phone',
			'title' => 'Title',
			'url' => 'Url',
			'thumb' => 'Thumb',
			'order_type' => 'Order Type',
			'description' => 'Description',
			'create_time' => 'Create Time',
			'quantity' => 'Quantity',
			'price' => 'Price',
			'amount' => 'Amount',
			'discount' => 'Discount',
			'status' => 'Status',
			'payment_time' => 'Payment Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('order_id',$this->order_id,true);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('thumb',$this->thumb,true);
		$criteria->compare('order_type',$this->order_type,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('quantity',$this->quantity);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('amount',$this->amount,true);
		$criteria->compare('discount',$this->discount,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('payment_time',$this->payment_time);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Orders the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getList($city_id,$token){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->addCondition("url='$token'");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getUserList($user_id,$first,$total){
        $db = Yii::app()->db->createCommand();
        $db->select('*');
        $db->from('{{orders}}');
        $db->andWhere('user_id=:user_id',array(':user_id'=>$user_id));
        $db->order('id desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getUserCountList($user_id){
        $db = Yii::app()->db->createCommand();
        $db->select('count(*) as count');
        $db->from('{{orders}}');
        $db->andWhere('user_id=:user_id',array(':user_id'=>$user_id));
        $info = $db->queryRow();
        return $info['count'];
    }
}
